package com.think.filter;

import com.alibaba.fastjson.JSONObject;
import com.think.exception.TimestampExecption;
import com.think.model.User;
import com.think.util.HttpServletRequestUtil;
import com.think.util.JSONObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2018/5/22 0022.
 */
public class WebUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    private static final Logger logger = LoggerFactory.getLogger(WebUsernamePasswordAuthenticationFilter.class);

    public WebUsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(new AntPathRequestMatcher("/login", "POST"));
        super.setAuthenticationManager(authenticationManager);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
//        User user1 = new ObjectMapper()
//                .readValue(httpServletRequest.getInputStream(), User.class);
//        logger.info("user1 = "  + user1);

        String result = HttpServletRequestUtil.readAsChars(httpServletRequest);
        logger.info("result = " + result);
        JSONObject jsonObject = JSONObjectUtil.entityToJSON(result);
        User user = JSONObjectUtil.jsonToBean(jsonObject, User.class);
        String username = user.getUsername();
        String password = user.getPassword();
        String timestamp = user.getTimestamp();
        if (timestamp == null || timestamp.length() == 0) {
            throw new TimestampExecption("timestamp is null");
        }
        username = username.trim();
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
        this.setDetails(httpServletRequest, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }

    protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
        authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
    }
    protected String getUsername(HttpServletRequest request) {
        return request.getParameter("username");
    }

    protected String getPassword(HttpServletRequest request) {
        return request.getParameter("password");
    }


}
